package com.jtech.together.view.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.jtech.together.R;
import com.jtech.together.mvp.contract.ModifyNicknameContract;
import com.jtech.together.mvp.presenter.ModifyNicknamePresenter;
import com.jtech.together.view.activity.base.BaseActivity;
import com.jtech.together.view.weight.dialog.CustomProgressDialog;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;

/**
 * 修改昵称
 * Created by wuxubaiyang on 2017/5/17.
 */
public class ModifyNicknameActivity extends BaseActivity implements ModifyNicknameContract.View, View.OnClickListener, TextView.OnEditorActionListener,
        Toolbar.OnMenuItemClickListener {
    public static final String KEY_DEFAULT_NICKNAME = "keyDefaultNickname";

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.textinputlayout_modify_nickname)
    TextInputLayout textInputLayout;

    private ModifyNicknameContract.Presenter presenter;

    @Override
    protected void initVariables(Bundle bundle) {
        //绑定P类
        presenter = new ModifyNicknamePresenter(getActivity(), this, bundle);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_modify_nickname);
        //设置标题栏
        setSupportActionBar(toolbar);
        //设置点击事件
        toolbar.setNavigationOnClickListener(this);
        //设置菜单选择事件
        toolbar.setOnMenuItemClickListener(this);
        //设置键盘动作监听
        textInputLayout.getEditText()
                .setOnEditorActionListener(this);
    }

    @Override
    protected void loadData() {
        //设置默认昵称
        setDefaultNickname(presenter.getDefaultNickname());
    }

    /**
     * 设置默认昵称
     *
     * @param nickname
     */
    private void setDefaultNickname(String nickname) {
        EditText editText = textInputLayout.getEditText();
        editText.setText(nickname);
        editText.setSelection(nickname.length());
    }

    /**
     * 提交
     *
     * @return
     */
    private boolean submit() {
        String content = textInputLayout.getEditText()
                .getText().toString();
        if (TextUtils.isEmpty(content)) {
            Snackbar.make(toolbar, "昵称不能为空", Snackbar.LENGTH_SHORT).show();
            return false;
        }
        //提交修改简介
        CustomProgressDialog.showProgressDialog(getActivity(), "正在编辑昵称");
        presenter.updateNickname(content);
        return true;
    }

    @Override
    public void updateSuccess(String nickname) {
        CustomProgressDialog.dismissProgressDialog();
        setResult(RESULT_OK);
        onBackPressed();
    }

    @Override
    public void updateFail(String error) {
        CustomProgressDialog.dismissProgressDialog();
        Snackbar.make(toolbar, error, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        toolbar.inflateMenu(R.menu.menu_done);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.done://完成按钮
                return submit();
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        //后退
        onBackPressed();
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            return submit();
        }
        return false;
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
        MobclickAgent.onPause(this);
    }
}